package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MainTask;
import com.ruoyi.system.service.IMainTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 主任务Controller
 * 
 * @author zhaoyu
 * @date 2025-06-18
 */
@RestController
@RequestMapping("/task/mainTask")
public class MainTaskController extends BaseController
{
    @Autowired
    private IMainTaskService mainTaskService;

    /**
     * 查询主任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(MainTask mainTask)
    {
        startPage();
        List<MainTask> list = mainTaskService.selectMainTaskList(mainTask);
        return getDataTable(list);
    }

    /**
     * 导出主任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:export')")
    @Log(title = "主任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MainTask mainTask)
    {
        List<MainTask> list = mainTaskService.selectMainTaskList(mainTask);
        ExcelUtil<MainTask> util = new ExcelUtil<MainTask>(MainTask.class);
        util.exportExcel(response, list, "主任务数据");
    }

    /**
     * 获取主任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId)
    {
        return success(mainTaskService.selectMainTaskByTaskId(taskId));
    }

    /**
     * 新增主任务
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:add')")
    @Log(title = "主任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MainTask mainTask)
    {
        return toAjax(mainTaskService.insertMainTask(mainTask));
    }

    /**
     * 修改主任务
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:edit')")
    @Log(title = "主任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MainTask mainTask)
    {
        return toAjax(mainTaskService.updateMainTask(mainTask));
    }

    /**
     * 删除主任务
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:remove')")
    @Log(title = "主任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return toAjax(mainTaskService.deleteMainTaskByTaskIds(taskIds));
    }

    /**
     * 检查主任务是否可以编辑
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:query')")
    @GetMapping("/checkEditable/{taskId}")
    public AjaxResult checkTaskEditable(@PathVariable Long taskId)
    {
        boolean editable = mainTaskService.checkTaskEditable(taskId);
        return AjaxResult.success(editable);
    }

    /**
     * 检查主任务是否可以删除
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:query')")
    @GetMapping("/checkDeletable/{taskId}")
    public AjaxResult checkTaskDeletable(@PathVariable Long taskId)
    {
        boolean deletable = mainTaskService.checkTaskDeletable(taskId);
        return AjaxResult.success(deletable);
    }

    /**
     * 获取主任务的子任务状态统计
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:query')")
    @GetMapping("/subTaskStatus/{taskId}")
    public AjaxResult getSubTaskStatus(@PathVariable Long taskId)
    {
        java.util.Map<String, Object> status = mainTaskService.getSubTaskStatus(taskId);
        return AjaxResult.success(status);
    }

    /**
     * 手动更新主任务状态（根据子任务状态）
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:edit')")
    @Log(title = "主任务状态更新", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus/{taskId}")
    public AjaxResult updateMainTaskStatus(@PathVariable Long taskId)
    {
        int result = mainTaskService.updateMainTaskStatusBySubTasks(taskId);
        return toAjax(result);
    }

    /**
     * 接收主任务
     */
    @PreAuthorize("@ss.hasPermi('task:mainTask:edit')")
    @PutMapping("/receive/{taskId}")
    public AjaxResult receiveMainTask(@PathVariable Long taskId)
    {
        return toAjax(mainTaskService.receiveMainTask(taskId));
    }

}
